package com.zzb.service.impl;

import com.github.pagehelper.PageHelper;
import com.zzb.DTO.ProductTypeDTO;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.ProductTypeVO;
import com.zzb.dao.ProductTypeDao;
import com.zzb.entity.ProductTypeEntity;
import com.zzb.service.ProductTypeService;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    ProductTypeEntity productTypeEntity = new ProductTypeEntity();
    @Resource
    ProductTypeDao productTypeDao;
    @Override
    public ResultDTO add(ProductTypeVO productTypeVO) {
        //数据复制
        BeanUtils.copyProperties(productTypeVO, productTypeEntity);
        //判断产品类型是否存在
        if (productTypeDao.findByName(productTypeEntity) == null) {//不存在
            return ResultDTO.getResult("success", productTypeDao.add(productTypeEntity));
        } else {//已存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO delete(ProductTypeVO productTypeVO) {
        //数据复制
        BeanUtils.copyProperties(productTypeVO, productTypeEntity);
        //判断产品类型是否存在
        if (productTypeDao.findById(productTypeEntity) != null) {//存在
            return ResultDTO.getResult("success", productTypeDao.delete(productTypeEntity));
        } else {//不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(ProductTypeVO productTypeVO) {
        BeanUtils.copyProperties(productTypeVO, productTypeEntity);
        productTypeEntity = productTypeDao.findById(productTypeEntity);
        if (productTypeEntity != null) {
            ProductTypeDTO productTypeDTO = new ProductTypeDTO();
            BeanUtils.copyProperties(productTypeEntity, productTypeDTO);
            return ResultDTO.getResult("success", productTypeDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findAll(ProductTypeVO productTypeVO) {
        //复制
        BeanUtils.copyProperties(productTypeVO,productTypeEntity);
        //查询数据条数
        int count = productTypeDao.count(productTypeEntity);
        if(productTypeEntity.getPage()!=null){
            //插件分页
            PageHelper.startPage(productTypeEntity.getPage(),productTypeEntity.getLimit());
        }
        //查找数据
        List<ProductTypeEntity> list = productTypeDao.findAll(productTypeEntity);
        //集合复制
        List<ProductTypeDTO> productTypeDTOList = Tool.copy(list, ProductTypeDTO.class);
        return ResultDTO.getResult(count, productTypeDTOList);
    }

    @Override
    public ResultDTO update(ProductTypeVO productTypeVO) {
        BeanUtils.copyProperties(productTypeVO, productTypeEntity);
        ProductTypeEntity findProductTypeEntity = productTypeDao.findByName(productTypeEntity);
        if (findProductTypeEntity == null) {//类型名称不存在或者没有修改名字
            return ResultDTO.getResult("success", productTypeDao.update(productTypeEntity));
        }
        String newName = productTypeEntity.getName();
        String oldName = findProductTypeEntity.getName();
        if (oldName.equals(newName)) {
            return ResultDTO.getResult("success", productTypeDao.update(productTypeEntity));
        }
        return ResultDTO.getResult("error", 0);
    }
}
